(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:PreorderoptCtrl
     * @description
     * # PreorderoptCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('PreorderoptCtrl', PreorderoptCtrl);

    PreorderoptCtrl.$inject = ['$scope', 'dataService', 'helper', 'dhfprinterService', '$timeout', '$interval', '$rootScope', '$state', 'moment', '$location'];

    function PreorderoptCtrl($scope, dataService, helper, dhfprinterService, $timeout, $interval, $rootScope, $state, moment, $location) {
        $scope.listAction = function() {
            $scope.getlist(1);
        }

        $scope.getlist = function() {
            var query = {};
            if ($scope.pre_order_opt_id) {
                query.query = {
                    pre_order_opt_id: $scope.pre_order_opt_id
                };
            }

            dataService.preorderoptGet(query).then(function(re1) {
                if (re1.subsys_opts.length) {
                    $scope.subsys_opt = re1.subsys_opts[0];
                    // console.error(" $scope.subsys_opt", $scope.subsys_opt);
                } else {
                    // console.error("11111111111111111");
                    if ($rootScope.store_id) {
                        $scope.addinit();
                    }
                }
            });
        }

        //添加数据 初始化
        $scope.addAction = function() {
            $scope.pre_order_opt_id = $location.search().pre_order_opt_id;
            if ($scope.pre_order_opt_id) {
                $scope.getlist();
            }

        }

        // schema_type_id
        $scope.saveData = function() {
            dataService.preoderoptUpdate($scope.subsys_opt).then(function(r) {
                if (r.preorder_opt_id) {
                    helper.myAlert("success", null, null, function() {
                        // $scope.stltodat();
                        $state.go("preorderopt.list");
                    });
                } else {
                    helper.myAlert("error");
                }
            })
        }

        //生成数据
        $scope.addinit = function() {
            dataService.preorderoptAdd({ store_id: $rootScope.store_id }).then(function(re1) {
                $scope.getlist(1);
            });
        }

        //生成数据
        $scope.stltodat = function() {
            var query = {
                page_no: 1,
                page_size: 100,
                query: { store_id: $rootScope.store_id }
            };
            dataService.schemaGet(query).then(function(re1) {
                if (re1.schemas) {
                    var schemas = re1.schemas;
                    _.map(schemas, function(da, ke) {
                        if (da) {
                            var qu = { "schema_id": da.schema_id };
                            dataService.stltodataAdd(qu).then(function(re2) {
                                console.error("re2", re2);
                            });
                        }
                    });
                }
            });
        }
    }
})();